package com.huilam.restactions.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huilam.restactions.common.util.HttpUtils;


public class HttpReqClient {
	
	private HttpServletRequest httpReq	= null;
	private HttpServletResponse httpRes	= null;
	
	private String ip 				= null;
	private String requestUri 		= null;
	private String requestMethod 	= null;
	private Properties restUrlParam	= null;
	private String[] requestPath 	= null;
	private boolean isAuthenticated = false;
	
	public HttpReqClient(ServletRequest req, ServletResponse res)
	{
		restUrlParam = new Properties();
		httpReq 	= (HttpServletRequest) req;
		httpRes		= (HttpServletResponse) res;
		//
		setRequestMethod(httpReq.getMethod());
		setIp(req.getRemoteAddr());	
		setRequestUri(httpReq.getRequestURI().substring(req.getServletContext().getContextPath().length()));
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String[] getRequestPath() {
		return requestPath;
	}

	public void setRequestPath(String[] requestPath) {
		this.requestPath = requestPath;
	}

	public boolean isAuthenticated() {
		return isAuthenticated;
	}

	public void setAuthenticated(boolean isAuthenticated) {
		this.isAuthenticated = isAuthenticated;
	}
	//
	
	public HttpServletRequest getHttpRequest()
	{
		return httpReq;
	}
	
	public HttpServletResponse getHttpResponse()
	{
		return httpRes;
	}
	
	//
	public void setRequestUri(String aReqUri) {
		
		if(aReqUri!=null && aReqUri.endsWith("/"))
			aReqUri = aReqUri.substring(0, aReqUri.length()-1);
		
		this.requestPath 	= HttpUtils.parseUri(aReqUri);
		this.requestUri 	= aReqUri;
	}
	public String getRequestMethod() {
		return requestMethod;
	}

	public void setRequestMethod(String requestMethod) {
		this.requestMethod = requestMethod;
	}

	public String getRequestUri() {
		return this.requestUri;
	}
	
	public void addRESTUrlParam(String aKey, String aValue)
	{
		restUrlParam.put(aKey, aValue);
	}
	
	public String getRESTUrlParam(String aKey)
	{
		return (String) restUrlParam.get(aKey);
	}	
	
	public String getParameter(String aParamName)
	{
		String sParamValue =  null;
		if(aParamName==null)
		{
			//get full content
			sParamValue = getStreamInStr(httpReq);
		}
		else
		{
			httpReq.getParameter(aParamName);
		}
		return sParamValue;
	}
	
	private String getStreamInStr(HttpServletRequest req) 
	{
		BufferedReader rdr = null;
		String sData = null;
		try{
			StringBuffer sb = new StringBuffer();
			rdr = new BufferedReader(new InputStreamReader(req.getInputStream()));
			String sTmp = null;
			while((sTmp=rdr.readLine())!=null)
			{
				sb.append(sTmp);
			}
			sData = sb.toString();
		}
		catch(IOException ex)
		{
			//ignore
		}
		finally
		{
			if(rdr!=null)
				try {
					rdr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		return sData;
	}
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("getIp=").append(getIp()).append("\n");
		sb.append("isAuthenticated=").append(isAuthenticated()).append("\n");
		sb.append("getRequestMethod=").append(getRequestMethod()).append("\n");
		sb.append("getRequestUri=").append(getRequestUri()).append("\n");
		sb.append("getRequestPath=");
		for(int i=0;i<getRequestPath().length;i++)
		{
			if(i>0)
				sb.append(",");
			sb.append(getRequestPath()[i]);
		}
		sb.append("\n");
		
		Enumeration e = restUrlParam.keys();
		while(e.hasMoreElements())
		{
			String sKey = (String) e.nextElement();
			sb.append("\ngetRESTUrlParam(").append(sKey).append(")=").append(restUrlParam.get(sKey));
		}
		sb.append("\n");
		
		e = httpReq.getParameterNames();
		while(e.hasMoreElements())
		{
			String sKey = (String) e.nextElement();
			sb.append("\ngetParameter(").append(sKey).append(")=").append(getParameter(sKey));
		}
		
		String sReqContent = getParameter(null);
		if(sReqContent!=null && sReqContent.length()>0)
		{
			sb.append("\ngetParameter(null)=").append(sReqContent).append(")");
		}
		
		return sb.toString();
	}
	

}
